
*--------------------------------------------------
* PROGRAM SETUP
*--------------------------------------------------
version 16
set more off
clear all
set linesize 80
macro drop _all
capture log close
set type double
di c(current_date) " " c(current_time)
local dt="`c(current_date)' `c(current_time)'"
local dt=subinstr("`dt'",":","",.)
local dt=subinstr("`dt'"," ","",.)
log using ./r1_gen_output.log, replace
di c(current_date) " " c(current_time)
* --------------------------------------------------
include ./0.config.do

* --------------------------------------------------
* CLEAN OUT
* --------------------------------------------------
shell rm $out/?*

* --------------------------------------------------
* LOAD DATA 
* --------------------------------------------------

* founding team 5 year outcomes
use $inConf/spd_founding_team_5yr, clear
bys yr: egen priorep20=xtile(lnfqreadj), nq(20)
bys yr: egen priorep20_ex=xtile(lnfqreadj_ex), nq(20)
bys yr: egen priorep20_kp=xtile(lnfqreadj_kp), nq(20)
tempfile ft5yr
save `ft5yr'

* founding team firm panel
use $inConf/spd_jcr, clear
replace death=1 if emp2f==0
rename death exit
replace rrev1 =. if age==0
gen lp1 = rrev1/emp1f
gen lnlp1 = ln(lp1)
egen Iind_yr = group(naics4 yr)
keep pfid yr exit age emp1f emp2f napay1f napay2f rrev1 rrev2 statef gr_emp lnlp1 Iind_yr 
tempfile firmpanel
save `firmpanel'

* abstract tasks
* PUBLIC DATA
import delimited "$inPublic/oes2007_soc2000dd_task_alm.csv", clear
gen absi = -rmti
collapse (sum) emp2007_oes, by(soc2000 absi)
drop if absi == .
egen thres = wpctile(absi), p(67) weights(emp2007_oes)
gen abst = absi >= thres
keep soc2000 abst
tempfile abst
save `abst'

* PUBLIC DATA
import delimited "$inPublic/oes2007_soc2000dd_task_alm.csv", clear
drop _merge
merge m:1 soc2000 using `abst'
keep if _m == 3
drop _m
gen emp_abst = emp2007_oes*abst
bys naics07 : egen totemp_abst = total(emp_abst)
bys naics07 : egen totemp = total(emp2007_oes)
gen sabst = totemp_abst/totemp
keep naics07 sabst
duplicates drop
tostring naics07, replace
rename naics07 naics4str
tempfile sabst
save `sabst'

* calculate averages of col at higher levels of NAICS to fill in missing values
use $inConf/skill_intensity.dta, clear
rename naics_birth naics07
forval i =2/4 {
    gen n`i' = substr(naics07,1,`i')
    bys n`i': egen n`i'm = mean(col)
    preserve
      keep n`i' n`i'm
      duplicates drop
      tempfile coln`i'
      save `coln`i''
    restore
}

* prep cpi 
* PUBLIC DATA
import sas using $inPublic/ipindex_updated, clear
replace ipi = ipi / 100
rename year yr
tsset yr
gen ipi_tm1 = L1.ipi
tempfile cpi
save `cpi'

* prep b2b classifciation
* PUBLIC DATA
use $inPublic/delgado_1998_2013, clear
keep naics2007 sc67 perc_supplies_to_pce traded
drop if naics2007=="#N/A"
bysort naics2007: keep if _n==1 
rename naics2007 naics07_birth
tempfile b2b_bridge
save `b2b_bridge'

* prep hp sectors
import sas using $inConf/hpsector_1990_2015, clear
rename hpsector hp
tempfile hp
save `hp'

* prep col %
use $inConf/skill_intensity.dta, clear
rename naics_birth naics07
tempfile colpct
save `colpct'

* prep hct
use $inConf/nondirectional_hct_naics.dta, clear
rename (prnaics naics hct) (hct_naics07 hct_fqnaics hct)
tempfile hct
save `hct'

* --------------------------------------------------
* PREP DEATH REGRESSION PANEL 
* --------------------------------------------------
use $inConf/death_matched_extended_panel, clear
* drop sole props and partnerships 
drop if lfo_birth == "P" | substr(lfo_birth,1,1) == "2"
* drop exits that occur before death
drop if deathyr >= yr & emp2f == 0
rename death firmdeath
* get shock year average age of active ft
gen qtime = deathqtr 
rename age fage
merge m:1 pfid qtime using $inConf/tempmatch, keep(1 3) keepusing(qtime pfid age) nogen
rename age actvage_shockyr
rename fage age
drop qtime

* fix indexing of revenue
merge m:1 yr using `cpi', keep(1 3) nogen
replace nrev2 =. if yr < 1997
drop rrev1 rrev2
gen rrev2 = nrev2 / ipi
gen rrev1 = nrev1 / ipi_tm1
* replace rev missing for problematic cases
replace rrev2=. if rrev2==0 & rrev1==. & firmdeath==1
replace rrev2=. if (emp2f==0 | emp2f==.) & rrev1==.
* replace rrev2 0 for exits that had revenue in t-1
replace rrev2=0 if (firmdeath==1 | emp2f==0) & !missing(rrev1) 
* missing lag rev for age = 0 
replace rrev1 =. if age==0
drop ipi ipi_tm1 

* hp sectors
rename naics4_birth naics4
merge m:1 naics4 using `hp', keep(1 3) nogen

* b2b classification
rename naics_birth naics07_birth
merge m:1 naics07_birth using `b2b_bridge', keep(1 3) nogen
* Adjusting for unmatched NAICS-6 (6 of them) by using averages of next highest-level nonmissing NAICS
* PUBLIC DATA
replace perc_supplies_to_pce = .1188 if substr(naics07_birth,1,2)=="11" 
replace perc_supplies_to_pce = .9116 if substr(naics07_birth,1,3)=="525" 
replace perc_supplies_to_pce = 1.0 if substr(naics07_birth,1,4)=="6111" 
replace perc_supplies_to_pce = .8737 if substr(naics07_birth,1,4)=="6112" 
replace perc_supplies_to_pce = 1.0 if substr(naics07_birth,1,4)=="8131" 
replace sc67=1 if sc67==. & perc_supplies_to_pce>=.67
replace sc67=0 if sc67==. & perc_supplies_to_pce<.67
* Define & Summary Stats on B2B (at shock year)
rename sc67 b2b
gen b2bcont = 1-perc_supplies_to_pce

rename naics07_birth naics_birth

sort pfid yr

* create analysis variables
gen year_to_death = gap + 5
gen emp = emp2f
gen pay = rapay2f
gen rev = rrev2
rename age firmage
rename avg_age avgage
gen ln_emp = ln(emp)
gen ln_rev = ln(rrev2)
gen ln_prod = ln(rrev2/emp)
gen ihs_emp = asinh(emp)
gen ihs_rev = asinh(rrev2)
gen ihs_revemp = ihs_rev - ihs_emp
replace ihs_revemp =. if missing(ihs_rev)
count if !missing(ihs_revemp)
count if !missing(ihs_rev)

* firm birth year attributes
preserve
    keep if firmage == 0
    keep pfid rrev2 emp yr actv_cnt
    rename rrev2 rrev
    gen prod = rrev / emp
    foreach v of varlist rrev emp yr prod actv_cnt {
        rename `v' `v'_birthyr
    }
    tempfile birth
    save `birth'
restore
merge m:1 pfid using `birth', nogen

* shock year attributes
preserve
    keep if gap==0
    keep pfid rrev2 emp yr payq1 firmage actv_cnt naics4 ln_emp
    rename rrev2 rrev
    gen prod = rrev / emp
    foreach v of varlist rrev emp yr prod firmage actv_cnt naics4 ln_emp {
        rename `v' `v'_shockyr
    }
    gen lq1ppe_shockyr = ln(payq1/emp)
    tempfile shocksize
    save `shocksize'
restore
merge m:1 pfid using `shocksize', nogen

* shock t-1
preserve
    keep if gap == -1
    keep pfid rrev2 emp yr actv_cnt
    rename rrev2 rrev
    gen prod = rrev / emp
    foreach v of varlist rrev emp yr prod actv_cnt {
        rename `v' `v'_shockyrtm1
    }
    tempfile shocktm1
    save `shocktm1'
restore
merge m:1 pfid using `shocktm1', nogen

* conditional-on-positive change variables
foreach v in emp rrev prod {
    gen denom_`v' = (`v'_birthyr + `v'_shockyrtm1)/2
    gen chg_`v' = (`v'_shockyrtm1 - `v'_birthyr)/denom_`v'
    drop denom_`v'
}

* create heterogeneous treatment variables and fixed effects
egen Ist = group(statef)
gen naics6_birth = naics_birth
drop naics07
gen naics07 = naics4
destring naics4, replace
gen post = year_to_death>=5
* deviated earnings of death shock worker
gen xdev_earn = (lnfqreadj - pearn)/actv_cnt
gen xdev_earn_post = xdev_earn*post
gen xdev_earn_treated_post = xdev_earn*treated*post
* small team indicator 
gen small = 1 if actv_cnt <= 5
replace small=0 if small==.

* get high tech flag
gen naics07_4 = naics07
merge m:1 naics07_4 using $inPublic/stemunion07, keep(1 3)
gen hitech = _merge==3

* firm FE
bys pfid: gen fid = _n==1 
replace fid = sum(fid)
* indicators of missingness for subsamples
gen haspe=!missing(xdev_earn)
gen hasrev=!missing(rrev2)
replace hasrev=1 if firmdeath==1 & !missing(rrev1)

* flag young shocked firms
gen y = firmage <=5 & gap==0
bys pfid: egen young = sum(y) 
drop y

* get firm age at death
preserve
    keep if deathyr == yr
    keep pfid firmage
    rename firmage firmage_at_deathyr
    tempfile firmage_at_deathyr
    save `firmage_at_deathyr', replace
restore
merge m:1 pfid using `firmage_at_deathyr', keep(1 3) nogen

* flag survivors 
preserve 
    keep if gap >= 0 & gap <=5
    keep if firmdeath == 1
    keep pfid
    gen survin5 = 0
    duplicates drop
    tempfile surv
    save `surv'
restore
merge m:1 pfid using `surv', nogen
replace survin5 = 1 if surv == .

gen r=uniform()

rename lnfqreadj lnfqreadj_at_death

*.. cumulative window (i.e., death BY year t)
foreach num of numlist 1/5 {
        gen death_in`num'=(firmdeath==1 & gap==`num')
}
egen temp_death_by1 = rowtotal(death_in1)
egen temp_death_by2 = rowtotal(death_in1 death_in2)
egen temp_death_by3 = rowtotal(death_in1 death_in2 death_in3)
egen temp_death_by4 = rowtotal(death_in1 death_in2 death_in3 death_in4)
egen temp_death_by5 = rowtotal(death_in1 death_in2 death_in3 death_in4 death_in5)

foreach num of numlist 1/5 {
    bys pfid: egen death_by`num'= max(temp_death_by`num')
    drop death_in`num' temp_death_by`num'
}

* additional sample restrictions to avoid implicit samples
drop if missing(lq1ppe_shockyr) | missing(ln_emp_shockyr)

* dhs measures of change from death shock year
gen dhs_emp = (emp-emp_shockyr)/(0.5*(emp+emp_shockyr))
gen dhs_rev = (rrev2-rrev_shockyr)/(0.5*(rrev2+rrev_shockyr))

* flag death shocks by median age
sum age_at_death if firmage_at_deathyr<=5, d
local agemd = `r(p50)'
preserve
    keep if firmage_at_deathyr<=5
    egen p48age = pctile(age_at_death), p(48)
    egen p52age = pctile(age_at_death), p(52)
    gen fzyagemd = age_at_death if age_at_death>=p48age & age_at_death<=p52age
    collapse (mean) fzyagemd
    local fzyagemd = fzyagemd[1]
restore
gen gtmed = age_at_death>`agemd'

rename (naics07 naics6_birth) (naics4str naics07)
* get extra het vars
* abstract task industries
merge m:1 naics4str using `sabst', keep(1 3) nogen
* college share by industry
merge m:1 naics07 using `colpct', keep(1 3) nogen keepusing(naics07 col)
* filling missings
foreach i in 4 3 2  {
    gen n`i' = substr(naics07,1,`i')
    merge m:1 n`i' using `coln`i'', keep(1 3) nogen
    replace col = n`i'm if missing(col) & !missing(n`i'm)
    drop n`i' n`i'm
}
gen hct_naics07 = substr(naics07,1,6) 
gen hct_fqnaics = substr(fqnaics,1,6)
merge m:1 hct_naics07 hct_fqnaics using `hct', keep(1 3) nogen
drop hct_naics07 hct_fqnaics
gen sprp = 1 if lfo_birth == "S" | lfo_birth == "10" | lfo_birth == "12"
replace sprp = 0 if sprp == .
replace sprp = . if lfo_birth == ""
* ensure sabst, sprp, and col have same samples
foreach v in sabst col sprp {
    replace `v' = . if missing(sabst)
    replace `v' = . if missing(col)
    replace `v' = . if missing(sprp)
}

* create interaction variables
foreach v in sabst col hct sprp {
    * mean deviate continuous measures
    if "`v'" != "sprp"  {
        su `v'
        gen mean_`v' = r(mean)
        replace  `v' = `v'-mean_`v'
    }

    * compute interactions
    gen `v'_post = `v'*post 
    gen `v'_treated_post = `v'*treated*post
    gen kftp_`v'_post = kft_prime*`v'*post
    gen kftp_`v'_treated_post = kft_prime*`v'*treated*post
}

gen fageshock_2_5 = firmage_shockyr >=2 & firmage_shockyr<=5 if firmage_at_deathyr<=5 & gap>=-5 & gap<=5

gen rev_empdth = (rrev2)/emp_shockyr
sum rev_empdth if gap==0 
gen rev_empdth_scl = rev_empdth/`r(mean)'

keep pfid fid yr statef deathyr firmage age_at_death lnfqreadj_at_death treated year_to_death firmdeath gap naics4 emp1f emp2f napay1f napay2f rrev1 rrev2 actv_cnt `lhsVars' post xdev_earn xdev_earn_post xdev_earn_treated_post kft_prime small hitech hp r hasrev haspe emp pay rev young *_shockyr Ist death_by* naics07 b2b b2bcont *_birthyr *_shockyrtm1 chg_* survin5 firmage_at_deathyr lfo_birth fqnaics dhs_emp dhs_rev gtmed sprp sabst col sprp *_post fageshock_2_5 rev_empdth rev_empdth_scl ihs* hct

tempfile dthreg_fnl
save `dthreg_fnl'
tab treated
sum ln* dhs*

* --------------------------------------------------
* PREP SECOND YEAR JOINER REGRESSION PANEL 
* --------------------------------------------------
use $inConf/death_matched_syjoiner_panel, clear
* drop sole props and partnerships 
drop if lfo_birth == "P" | substr(lfo_birth,1,1) == "2"
* drop exits that occur before exit
drop if deathyr >= yr & emp2f == 0
rename death firmdeath

* fix indexing of revenue
merge m:1 yr using `cpi', keep(1 3) nogen
replace nrev2 =. if yr < 1997
drop rrev1 rrev2
gen rrev2 = nrev2 / ipi
gen rrev1 = nrev1 / ipi_tm1
* replace rev missing for problematic cases
replace rrev2=. if rrev2==0 & rrev1==. & firmdeath==1
replace rrev2=. if (emp2f==0 | emp2f==.) & rrev1==.
* replace rrev2 0 for exits that had revenue in t-1
replace rrev2=0 if (firmdeath==1 | emp2f==0) & !missing(rrev1) 
* missing lag rev for age = 0 
replace rrev1 =. if age==0
drop ipi ipi_tm1 

sort pfid yr

* create variables
gen year_to_death = gap + 5
gen emp = emp2f
gen pay = rapay2f
gen rev = rrev2
rename age firmage
rename avg_age avgage
gen ln_emp = ln(emp)
gen ln_rev = ln(rrev2)
gen ln_prod = ln(rrev2/emp)
gen ihs_emp = asinh(emp)
gen ihs_rev = asinh(rrev2)
gen ihs_revemp = ihs_rev - ihs_emp
replace ihs_revemp =. if missing(ihs_rev)
count if !missing(ihs_revemp)
count if !missing(ihs_rev)

* shock year attributes
preserve
    keep if gap==0
    keep pfid rrev2 emp yr payq1 firmage naics4
    rename rrev2 rrev
    foreach v of varlist rrev emp yr payq1 firmage naics4 {
        rename `v' `v'_shockyr
    }
    gen lq1ppe_shockyr = ln(payq1/emp)
    tempfile syjshocksize
    save `syjshocksize'
restore
merge m:1 pfid using `syjshocksize', nogen

* state fixed effects
egen Ist = group(statef)
drop naics07
gen naics07 = naics4
destring naics4, replace
gen post = year_to_death>=5
bys pfid: gen fid = _n==1 
replace fid = sum(fid)
gen hasrev=!missing(rrev2)
replace hasrev=1 if firmdeath==1 & !missing(rrev1)

* get firm age at death
preserve
    keep if deathyr == yr
    keep pfid firmage
    rename firmage firmage_at_deathyr
    tempfile firmage_at_deathyr
    save `firmage_at_deathyr', replace
restore
merge m:1 pfid using `firmage_at_deathyr', keep(1 3) nogen

gen r=uniform()

* dhs growth variables
gen dhs_emp = (emp-emp_shockyr)/(0.5*(emp+emp_shockyr))
gen dhs_rev = (rrev2-rrev_shockyr)/(0.5*(rrev2+rrev_shockyr))

tempfile dthregsyj_fnl
save `dthregsyj_fnl'

* --------------------------------------------------
* ESTIMATE REGRESSIONS
* --------------------------------------------------
local ogrp = 0
foreach v in dhs_emp dhs_rev {
    local o = 0
    local file = "`v'"
    local ogrp = `ogrp' + 1
    * --------------------------------------------------
    * FOUNDER EJ
    * --------------------------------------------------
    local o = `o' + 1
    shell echo "`ogrp'.`o' FDR & EJ `v' (T3)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
        reghdfe `v' 
        i.post
        i.post#i.kft_prime
        i.post#i.treated
        i.post#i.treated#i.kft_prime
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    * --------------------------------------------------
    * FOUNDER EJ LOG SAMPLE
    * --------------------------------------------------
    local o = `o' + 1
    shell echo "`ogrp'.`o' FDR & EJ LOG SAMPLE>0 `v' (TE2)">> $out/readme.txt
    use `dthreg_fnl', clear
    if "`v'"=="dhs_emp" | "`v'"=="ihs_emp" {
        keep if !missing(ln_emp)
    }
    if "`v'"=="dhs_rev" | "`v'"=="ihs_rev" {
        keep if !missing(ln_rev)
    }
    #delimit;
        reghdfe `v' 
        i.post
        i.post#i.kft_prime
        i.post#i.treated
        i.post#i.treated#i.kft_prime
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    * --------------------------------------------------
    * YOUNG 
    * --------------------------------------------------
    local o = `o' + 1
    shell echo "`ogrp'.`o' YOUNG `v' (T4)">> $out/readme.txt
    use `dthreg_fnl', clear

    #delimit;
        reghdfe `v'  
        i.post
        i.post#i.treated 
        i.post#i.kft_prime 
        i.post#i.young
        i.post#i.treated#i.kft_prime
        i.post#i.treated#i.young
        i.post#i.young#i.kft_prime
        i.post#i.treated#i.young#i.kft_prime
        if gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    * --------------------------------------------------
    * B2B 
    * --------------------------------------------------
    local o = `o' + 1
    shell echo "`ogrp'.`o' B2B `v' (T4)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
        reghdfe `v'  
        i.post
        i.post#i.treated 
        i.post#i.kft_prime 
        i.post#i.b2b
        i.post#i.treated#i.kft_prime
        i.post#i.treated#i.b2b
        i.post#i.b2b#i.kft_prime
        i.post#i.treated#i.b2b#i.kft_prime
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    * --------------------------------------------------
    * SMALL
    * --------------------------------------------------
    local o = `o' + 1
    shell echo "`ogrp'.`o' SMALL `v' (T4)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
        reghdfe `v'  
        i.post
        i.post#i.treated 
        i.post#i.kft_prime 
        i.post#i.small
        i.post#i.treated#i.kft_prime
        i.post#i.treated#i.small
        i.post#i.small#i.kft_prime
        i.post#i.treated#i.small#i.kft_prime
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    * --------------------------------------------------
    * SOLE PROP
    * --------------------------------------------------
    local o = `o' + 1
    shell echo "`ogrp'.`o' SOLE PROP `v' (T4)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
        reghdfe `v'  
        i.post
        i.post#i.treated 
        i.post#i.kft_prime 
        i.post#i.sprp
        i.post#i.treated#i.kft_prime
        i.post#i.treated#i.sprp
        i.post#i.sprp#i.kft_prime
        i.post#i.treated#i.sprp#i.kft_prime
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    * --------------------------------------------------
    * COL 
    * --------------------------------------------------
    local o = `o' + 1
    shell echo "`ogrp'.`o' COL `v' (T5)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
        reghdfe `v'  
        i.post
        i.post#i.treated 
        i.post#i.kft_prime 
        col_post
        i.post#i.treated#i.kft_prime
        col_treated_post
        kftp_col_post
        kftp_col_treated_post
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    * --------------------------------------------------
    * HT 
    * --------------------------------------------------
    local o = `o' + 1
    shell echo "`ogrp'.`o' HT `v' (T5)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
        reghdfe `v'  
        i.post
        i.post#i.treated 
        i.post#i.kft_prime 
        i.post#i.hitech
        i.post#i.treated#i.kft_prime
        i.post#i.treated#i.hitech
        i.post#i.hitech#i.kft_prime
        i.post#i.treated#i.hitech#i.kft_prime
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    * --------------------------------------------------
    * SABST 
    * --------------------------------------------------
    local o = `o' + 1
    shell echo "`ogrp'.`o' SABST `v' (T5)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
        reghdfe `v'  
        i.post
        i.post#i.treated 
        i.post#i.kft_prime 
        sabst_post
        i.post#i.treated#i.kft_prime
        sabst_treated_post
        kftp_sabst_post
        kftp_sabst_treated_post
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    * --------------------------------------------------
    * HCT 
    * --------------------------------------------------
    local o = `o' + 1
    shell echo "`ogrp'.`o' HCT `v' (T5)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
        reghdfe `v'  
        i.post
        i.post#i.treated 
        i.post#i.kft_prime 
        hct_post 
        hct_treated_post 
        kftp_hct_post 
        kftp_hct_treated_post
        i.post#i.treated#i.kft_prime
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    * --------------------------------------------------
    * PRIOR EARNINGS 
    * --------------------------------------------------
    local o = `o' + 1
    shell echo "`ogrp'.`o' PRIOR EARN `v' (TF1)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
        reghdfe `v' 
        i.post
        xdev_earn_post
        i.post#i.treated
        xdev_earn_treated_post
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    * --------------------------------------------------
    * HP 
    * --------------------------------------------------
    local o = `o' + 1
    shell echo "`ogrp'.`o' HP `v' (TA6)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
        reghdfe `v'  
        i.post
        i.post#i.treated 
        i.post#i.kft_prime 
        i.post#i.hp
        i.post#i.treated#i.kft_prime
        i.post#i.treated#i.hp
        i.post#i.hp#i.kft_prime
        i.post#i.treated#i.hp#i.kft_prime
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    * --------------------------------------------------
    * EVENT STUDY 
    * --------------------------------------------------
    local o = `o' + 1
    shell echo "`ogrp'.`o' EJ EVENT `v' (F2)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
        reghdfe `v' 
        ib4.year_to_death
        ib4.year_to_death#i.treated
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5 & kft_prime==0, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    local o = `o' + 1
    shell echo "`ogrp'.`o' FDR EVENT `v' (F2)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
        reghdfe `v' 
        ib4.year_to_death
        ib4.year_to_death#i.treated
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5 & kft_prime==1, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    * --------------------------------------------------
    * EVENT STUDY SECOND YEAR 
    * --------------------------------------------------
    
    local o = `o' + 1
    shell echo "`ogrp'.`o' SYJ `v' (F4)">> $out/readme.txt
    use `dthregsyj_fnl', clear
    #delimit;
        reghdfe `v' 
        ib4.year_to_death
        ib4.year_to_death#i.treated
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'
    
    * --------------------------------------------------
    * EVENT STUDY PERSISTENCE FIGURE 7
    * --------------------------------------------------
    local o = `o' + 1
    shell echo "`ogrp'.`o' EJ PERSIST `v' (FB9)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
        reghdfe `v' 
        ib4.year_to_death
        ib4.year_to_death#i.treated
        if firmage_at_deathyr<=10 & gap>=-5 & gap<=10 & kft_prime==0, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    local o = `o' + 1
    shell echo "`ogrp'.`o' FDR PERSIST `v' (FB9)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
        reghdfe `v' 
        ib4.year_to_death
        ib4.year_to_death#i.treated
        if firmage_at_deathyr<=10 & gap>=-5 & gap<=10 & kft_prime==1, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    * --------------------------------------------------
    * OLD INITIAL TEAM
    * --------------------------------------------------
    local o = `o' + 1
    shell echo "`ogrp'.`o' OLD `v' (TG1)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
    reghdfe `v'
        i.post
        i.post#i.treated 
        i.post#i.kft_prime 
        i.post#i.gtmed
        i.post#i.treated#i.kft_prime
        i.post#i.treated#i.gtmed
        i.post#i.gtmed#i.kft_prime
        i.post#i.treated#i.gtmed#i.kft_prime
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    * --------------------------------------------------
    * FE ROBUST
    * --------------------------------------------------
    local o = `o' + 1
    shell echo "`ogrp'.`o' FE ROBUST `v' (A2)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
    reghdfe `v'
        i.post
        i.post#i.treated 
        i.post#i.kft_prime 
        i.post#i.treated#i.kft_prime
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#firmage yr fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    local o = `o' + 1
    shell echo "`ogrp'.`o' FE ROBUST `v' (A2)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
    reghdfe `v'
        i.post
        i.post#i.treated 
        i.post#i.kft_prime 
        i.post#i.treated#i.kft_prime
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#firmage naics4#yr fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    local o = `o' + 1
    shell echo "`ogrp'.`o' FE ROBUST `v' (A2)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
    reghdfe `v'
        i.post
        i.post#i.treated 
        i.post#i.kft_prime 
        i.post#i.treated#i.kft_prime
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#firmage#yr fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    local o = `o' + 1
    shell echo "`ogrp'.`o' FE ROBUST `v' (A3)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
    reghdfe `v'
        i.post
        i.post#i.treated 
        i.post#i.kft_prime 
        i.post#i.b2b
        i.post#i.treated#i.kft_prime
        i.post#i.treated#i.b2b
        i.post#i.b2b#i.kft_prime
        i.post#i.treated#i.b2b#i.kft_prime
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#firmage yr fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    local o = `o' + 1
    shell echo "`ogrp'.`o' FE ROBUST `v' (A3)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
    reghdfe `v'
        i.post
        i.post#i.treated 
        i.post#i.kft_prime 
        i.post#i.b2b
        i.post#i.treated#i.kft_prime
        i.post#i.treated#i.b2b
        i.post#i.b2b#i.kft_prime
        i.post#i.treated#i.b2b#i.kft_prime
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#firmage naics4#yr fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    local o = `o' + 1
    shell echo "`ogrp'.`o' FE ROBUST `v' (A3)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
    reghdfe `v'
        i.post
        i.post#i.treated 
        i.post#i.kft_prime 
        i.post#i.b2b
        i.post#i.treated#i.kft_prime
        i.post#i.treated#i.b2b
        i.post#i.b2b#i.kft_prime
        i.post#i.treated#i.b2b#i.kft_prime
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#firmage#yr fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'

    * --------------------------------------------------
    * VERY YOUNG FIRM ROBUST
    * --------------------------------------------------
    local o = `o' + 1
    shell echo "`ogrp'.`o' VERY YOUNG `v' (TA4)">> $out/readme.txt
    use `dthreg_fnl', clear
    #delimit;
    reghdfe `v'
        i.post
        i.post#i.treated 
        i.post#i.kft_prime 
        i.post#i.fageshock_2_5
        i.post#i.treated#i.kft_prime
        i.post#i.treated#i.fageshock_2_5
        i.post#i.fageshock_2_5#i.kft_prime
        i.post#i.treated#i.fageshock_2_5#i.kft_prime
        if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
    #delimit cr
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'
}


local o = 0
local ogrp = `ogrp' + 1
local file = "extras"
* --------------------------------------------------
* EVENT STUDY FOR LOGS
* --------------------------------------------------
local o = `o' + 1
shell echo "`ogrp'.`o' EJ EVENT ln_emp (F3)">> $out/readme.txt
use `dthreg_fnl', clear
#delimit;
    reghdfe ln_emp
    ib4.year_to_death
    ib4.year_to_death#i.treated
    if firmage_at_deathyr<=5 & gap>=-5 & gap<=5 & kft_prime==0, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
#delimit cr
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 1 `er2' `en' `file' `ogrp' `o'

local o = `o' + 1
shell echo "`ogrp'.`o' FDR EVENT ln_emp (F3)">> $out/readme.txt
use `dthreg_fnl', clear
#delimit;
    reghdfe ln_emp 
    ib4.year_to_death
    ib4.year_to_death#i.treated
    if firmage_at_deathyr<=5 & gap>=-5 & gap<=5 & kft_prime==1, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
#delimit cr
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 1 `er2' `en' `file' `ogrp' `o'

local o = `o' + 1
shell echo "`ogrp'.`o' EJ EVENT ln_rev (F3)">> $out/readme.txt
use `dthreg_fnl', clear
#delimit;
    reghdfe ln_rev
    ib4.year_to_death
    ib4.year_to_death#i.treated
    if firmage_at_deathyr<=5 & gap>=-5 & gap<=5 & kft_prime==0, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
#delimit cr
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 1 `er2' `en' `file' `ogrp' `o'

local o = `o' + 1
shell echo "`ogrp'.`o' FDR EVENT ln_rev (F3)">> $out/readme.txt
use `dthreg_fnl', clear
#delimit;
    reghdfe ln_rev 
    ib4.year_to_death
    ib4.year_to_death#i.treated
    if firmage_at_deathyr<=5 & gap>=-5 & gap<=5 & kft_prime==1, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
#delimit cr
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 1 `er2' `en' `file' `ogrp' `o'

local o = `o' + 1
shell echo "`ogrp'.`o' SYJ ln_emp (B8)">> $out/readme.txt
use `dthregsyj_fnl', clear
#delimit;
    reghdfe ln_emp
    ib4.year_to_death
    ib4.year_to_death#i.treated
    if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
#delimit cr
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 1 `er2' `en' `file' `ogrp' `o'

local o = `o' + 1
shell echo "`ogrp'.`o' SYJ ln_rev (B8)">> $out/readme.txt
use `dthregsyj_fnl', clear
#delimit;
    reghdfe ln_rev
    ib4.year_to_death
    ib4.year_to_death#i.treated
    if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
#delimit cr
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 1 `er2' `en' `file' `ogrp' `o'

* --------------------------------------------------
* REVEMP, FIGURE A4
* --------------------------------------------------
local o = `o' + 1
shell echo "`ogrp'.`o' dhs_rev_emp EJ (FB6)">> $out/readme.txt
use `dthreg_fnl', clear
gen dhs_rev_emp = dhs_rev - dhs_emp
#delimit;
    reghdfe dhs_rev_emp
    ib4.year_to_death
    ib4.year_to_death#i.treated
    if firmage_at_deathyr<=5 & gap>=-5 & gap<=5 & kft_prime==0, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
#delimit cr
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 1 `er2' `en' `file' `ogrp' `o'

local o = `o' + 1
shell echo "`ogrp'.`o' dhs_rev_emp FDR (FB6)">> $out/readme.txt
use `dthreg_fnl', clear
gen dhs_rev_emp = dhs_rev - dhs_emp
#delimit;
    reghdfe dhs_rev_emp
    ib4.year_to_death
    ib4.year_to_death#i.treated
    if firmage_at_deathyr<=5 & gap>=-5 & gap<=5 & kft_prime==1, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
#delimit cr
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 1 `er2' `en' `file' `ogrp' `o'

* --------------------------------------------------
* FOUNDER EJ OTHER VARS (NEW APPENDIX)
* --------------------------------------------------
local o = `o' + 1
shell echo "`ogrp'.`o' ihs(emp) (TE1)">> $out/readme.txt
use `dthreg_fnl', clear
#delimit;
    reghdfe ihs_emp
    i.post
    i.post#i.kft_prime
    i.post#i.treated
    i.post#i.treated#i.kft_prime
    if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
#delimit cr
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 1 `er2' `en' `file' `ogrp' `o'

local o = `o' + 1
shell echo "`ogrp'.`o' ihs(rev) (TE1)">> $out/readme.txt
use `dthreg_fnl', clear
#delimit;
    reghdfe ihs_rev
    i.post
    i.post#i.kft_prime
    i.post#i.treated
    i.post#i.treated#i.kft_prime
    if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
#delimit cr
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 1 `er2' `en' `file' `ogrp' `o'

local o = `o' + 1
shell echo "`ogrp'.`o' rev_empdth (TE1)">> $out/readme.txt
use `dthreg_fnl', clear
#delimit;
    reghdfe rev_empdth
    i.post
    i.post#i.kft_prime
    i.post#i.treated
    i.post#i.treated#i.kft_prime
    if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
#delimit cr
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 1 `er2' `en' `file' `ogrp' `o'

local o = `o' + 1
shell echo "`ogrp'.`o' rev_empdth scaled (TE1)">> $out/readme.txt
use `dthreg_fnl', clear
#delimit;
    reghdfe rev_empdth_scl
    i.post
    i.post#i.kft_prime
    i.post#i.treated
    i.post#i.treated#i.kft_prime
    if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
#delimit cr
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 1 `er2' `en' `file' `ogrp' `o'

local o = `o' + 1
shell echo "`ogrp'.`o' ln(emp) (T3, TE2)">> $out/readme.txt
use `dthreg_fnl', clear
#delimit;
    reghdfe ln_emp
    i.post
    i.post#i.kft_prime
    i.post#i.treated
    i.post#i.treated#i.kft_prime
    if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
#delimit cr
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 1 `er2' `en' `file' `ogrp' `o'

local o = `o' + 1
shell echo "`ogrp'.`o' ln(rev) (T3, TE2)">> $out/readme.txt
use `dthreg_fnl', clear
#delimit;
    reghdfe ln_rev
    i.post
    i.post#i.kft_prime
    i.post#i.treated
    i.post#i.treated#i.kft_prime
    if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
#delimit cr
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 1 `er2' `en' `file' `ogrp' `o'

local o = `o' + 1
shell echo "`ogrp'.`o' rev_empdth for ln(rev) sample (TE2)">> $out/readme.txt
use `dthreg_fnl', clear
#delimit;
    reghdfe rev_empdth
    i.post
    i.post#i.kft_prime
    i.post#i.treated
    i.post#i.treated#i.kft_prime
    if firmage_at_deathyr<=5 & gap>=-5 & gap<=5 & !missing(ln(rev)), absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
#delimit cr
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 1 `er2' `en' `file' `ogrp' `o'

local o = `o' + 1
shell echo "`ogrp'.`o' rev_empdth scaled for ln(rev) sample (TE2)">> $out/readme.txt
use `dthreg_fnl', clear
#delimit;
    reghdfe rev_empdth_scl
    i.post
    i.post#i.kft_prime
    i.post#i.treated
    i.post#i.treated#i.kft_prime
    if firmage_at_deathyr<=5 & gap>=-5 & gap<=5 & !missing(ln(rev)), absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
#delimit cr
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 1 `er2' `en' `file' `ogrp' `o'

* --------------------------------------------------
* MATCH BALANCE
* --------------------------------------------------
local o = `o' + 1
shell echo "`ogrp'.`o' Match Balance (T1)">> $out/readme.txt
use if gap == 0 & firmage_at_deathyr<=5 using `dthreg_fnl', clear
est clear
estpost ttest firmage emp ln_emp ln_rev ln_prod actvage_shockyr, by(treated)
matrix A = e(mu_1)
drop _all
captureMatrix mean0
tempfile ttestOutput
save `ttestOutput'
matrix A = e(mu_2)
drop _all
captureMatrix mean1
merge 1:1 var using `ttestOutput', nogen
save `ttestOutput', replace
matrix A = e(b)
drop _all
captureMatrix diff
merge 1:1 var using `ttestOutput', nogen
save `ttestOutput', replace
matrix A = e(se)
drop _all
captureMatrix se
merge 1:1 var using `ttestOutput', nogen
save `ttestOutput', replace
matrix A = e(t)
drop _all
captureMatrix t
merge 1:1 var using `ttestOutput', nogen
save `ttestOutput', replace
order var mean1 mean0 diff t se
keep var mean1 mean0 diff se
export excel using $out/`file'.xlsx, sheet(o`ogrp'.`o') firstrow(variables) sheetmodify keepcellfmt

* --------------------------------------------------
* DEATH LPM
* --------------------------------------------------
foreach num of numlist 1/5 {
    local o = `o' + 1
    shell echo "`ogrp'.`o' Death LPM EJ, t+`num' (T2)">> $out/readme.txt
    use if gap==0 & firmage_at_deathyr<=5 using `dthreg_fnl', clear
    reghdfe death_by`num' treated lq1ppe_shockyr ln_emp_shockyr if kft_prime==0, absorb(naics4#yr Ist firmage_shockyr) vce(cluster naics4) keepsingletons
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'
}
foreach num of numlist 1/5 {
    local o = `o' + 1
    shell echo "`ogrp'.`o' Death LPM FDR, t+`num' (T2)">> $out/readme.txt
    use if gap==0 & firmage_at_deathyr<=5 using `dthreg_fnl', clear
    reghdfe death_by`num' treated lq1ppe_shockyr ln_emp_shockyr if kft_prime==1, absorb(naics4#yr Ist firmage_shockyr) vce(cluster naics4) keepsingletons
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 1 `er2' `en' `file' `ogrp' `o'
}

* --------------------------------------------------
* CONDITIONAL ON POSITIVE ANALYSIS
* --------------------------------------------------
foreach v in emp rrev {
    local o = `o' + 1
    shell echo "`ogrp'.`o' Cond On Pos `v', survin5 w/ FE (TA1)">> $out/readme.txt

    use if gap==0 & survin5==1 & firmage_at_deathyr<=5 using `dthreg_fnl', clear
    gen ln_`v'0 = ln(`v'_birthyr)
    * show with initial level for the log
    reghdfe chg_`v' treated, absorb(naics4 yr_birthyr firmage) vce(robust)
    local er2 = `e(r2)'
    local en = `e(N)'
    parmest, saving($out/hold.dta,replace)
    saveRegs 0 `er2' `en' `file' `ogrp' `o'
}

* --------------------------------------------------
* SKILL MEASURE CORRELATIONS
* --------------------------------------------------
local o = `o' + 1
shell echo "`ogrp'.`o' Skill measure correlations (TA5)">> $out/readme.txt
use if firmage_at_deathyr<=5 using `dthreg_fnl', clear
keep if !missing(hitech) & !missing(col) & !missing(sabst)
pwcorr hitech sabst col
putexcel set "hold", replace
putexcel A1=matrix(r(C))
import excel using hold.xlsx, clear 
rename (A B C) (hitech sabst col) 
replace sabst = . if _n==1
replace col = . if _n==1
replace col = . if _n==2
gen var = ""
replace var = "hitech" if _n==1
replace var = "sabst" if _n==2
replace var = "col" if _n==3
order var
export excel using $out/`file'.xlsx, sheet(o`ogrp'.`o') firstrow(variables) sheetmodify keepcellfmt

* --------------------------------------------------
* PRIOR EARNINGS AND 5-YEAR EMPLOYMENT GROWTH
* --------------------------------------------------
local o = `o' + 1
shell echo "`ogrp'.`o' EJ Prior Earnings & Emp Growth (F1A)">> $out/readme.txt

use pfid yr gr_emp5 ln_emp0 priorep20 priorep20_kp priorep20_ex Iind_yr death5 if yr<=2010 & death5==0 using `ft5yr', clear
areg gr_emp5 ib(1).priorep20_ex ln_emp0, r absorb(Iind_yr)
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 0 `er2' `en' `file' `ogrp' `o'

local o = `o' + 1
shell echo "`ogrp'.`o' FDR Prior Earnings & Emp Growth (F1A)">> $out/readme.txt

use pfid yr gr_prod5 ln_emp0 priorep20 priorep20_kp priorep20_ex Iind_yr death5 ln_lp0 if yr<=2010 & death5==0 using `ft5yr', clear
areg gr_prod5 ib(1).priorep20_kp ln_lp0, r absorb(Iind_yr)
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 0 `er2' `en' `file' `ogrp' `o'

* --------------------------------------------------
* PRIOR EARNINGS AND 5-YEAR PRODUCTIVITY GROWTH 
* --------------------------------------------------
local o = `o' + 1
shell echo "`ogrp'.`o' EJ Prior Earnings & Prod Growth (F1B)">> $out/readme.txt

use pfid yr gr_prod5 ln_emp0 priorep20 priorep20_kp priorep20_ex Iind_yr death5 ln_lp0 if yr<=2010 & death5==0 using `ft5yr', clear
areg gr_prod5 ib(1).priorep20_ex ln_lp0, r absorb(Iind_yr)
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 0 `er2' `en' `file' `ogrp' `o'

local o = `o' + 1
shell echo "`ogrp'.`o' FDR Prior Earnings & Prod Growth (F1B)">> $out/readme.txt

use pfid yr gr_prod5 ln_emp0 priorep20 priorep20_kp priorep20_ex Iind_yr death5 ln_lp0 if yr<=2010 & death5==0 using `ft5yr', clear
areg gr_prod5 ib(1).priorep20_kp ln_lp0, r absorb(Iind_yr)
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 0 `er2' `en' `file' `ogrp' `o'

* --------------------------------------------------
* PRIOR EARNINGS AND FIRM EXIT
* --------------------------------------------------
local o = `o' + 1
shell echo "`ogrp'.`o' EJ Prior Earnings & Exit (F1C)">> $out/readme.txt

use pfid yr death5 ln_emp0 priorep20 priorep20_kp priorep20_ex Iind_yr ln_lp0 if yr<=2010 using `ft5yr', clear
areg death5 ib(1).priorep20_ex ln_lp0, r absorb(Iind_yr)
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 0 `er2' `en' `file' `ogrp' `o'

local o = `o' + 1
shell echo "`ogrp'.`o' FDR Prior Earnings & Exit (F1C)">> $out/readme.txt

use pfid yr death5 ln_emp0 priorep20 priorep20_kp priorep20_ex Iind_yr ln_lp0 if yr<=2010 using `ft5yr', clear
areg death5 ib(1).priorep20_kp ln_lp0, r absorb(Iind_yr)
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 0 `er2' `en' `file' `ogrp' `o'

* --------------------------------------------------
* ATTRITION COUNTS
* --------------------------------------------------
use `ft5yr', clear
collapse (mean) actv_1_adj actv_1_adj_ex actv_1_adj_kp  actv_2_adj actv_2_adj_ex actv_2_adj_kp  actv_3_adj actv_3_adj_ex actv_3_adj_kp  actv_4_adj actv_4_adj_ex actv_4_adj_kp  actv_5_adj actv_5_adj_ex actv_5_adj_kp  actv_6_adj actv_6_adj_ex actv_6_adj_kp  actv_cnt_1 actv_cnt_2 actv_cnt_3 actv_cnt_4 actv_cnt_5 actv_cnt_6 actv_cnt_1_ex actv_cnt_1_kp  actv_cnt_2_ex actv_cnt_2_kp  actv_cnt_3_ex actv_cnt_3_kp  actv_cnt_4_ex actv_cnt_4_kp  actv_cnt_5_ex actv_cnt_5_kp  actv_cnt_6_ex actv_cnt_6_kp 
rename (actv_cnt_1 actv_cnt_2 actv_cnt_3 actv_cnt_4 actv_cnt_5 actv_cnt_6 actv_cnt_1_ex actv_cnt_1_kp  actv_cnt_2_ex actv_cnt_2_kp  actv_cnt_3_ex actv_cnt_3_kp  actv_cnt_4_ex actv_cnt_4_kp  actv_cnt_5_ex actv_cnt_5_kp  actv_cnt_6_ex actv_cnt_6_kp ) (actv_1_cnt actv_2_cnt actv_3_cnt actv_4_cnt actv_5_cnt actv_6_cnt actv_1_cnt_ex actv_1_cnt_kp  actv_2_cnt_ex actv_2_cnt_kp  actv_3_cnt_ex actv_3_cnt_kp  actv_4_cnt_ex actv_4_cnt_kp  actv_5_cnt_ex actv_5_cnt_kp  actv_6_cnt_ex actv_6_cnt_kp )
tempfile actv
save `actv'

* container 
clear 
set obs 1
gen yr = .
tempfile actvlong
save `actvlong'

local suffix = "adj_ex adj_kp adj cnt cnt_ex cnt_kp "
foreach s of local suffix {
    use `actv', clear
    gen lbl="us"
    keep lbl actv_*_`s'
    foreach i of numlist 1(1)6 {
        rename actv_`i'_`2' actv_`i' 
    }
    reshape long actv_, i(lbl) j(yr)
    rename actv_ `s'
    merge 1:1 yr using `actvlong'
    drop _merge
    save `actvlong', replace
}
drop if missing(yr)
drop lbl
save `actvlong', replace

use `actvlong', clear
keep yr adj adj_ex adj_kp 
rename adj priore
rename adj_ex prioreex
rename adj_kp  priorekft
replace yr = yr - 1

local o = `o' + 1
shell echo "`ogrp'.`o' Attrition and Prior Earnings (B4)">> $out/readme.txt
keep yr priorekft prioreex
export excel using $out/`file'.xlsx, sheet(o`ogrp'.`o') firstrow(variables) sheetmodify keepcellfmt

use `actvlong', clear
keep yr cnt cnt_ex cnt_kp 
rename cnt_ex cntex
rename cnt_kp  cntkp
replace yr = yr - 1

local o = `o' + 1
shell echo "`ogrp'.`o' Attrition Counts (B4)">> $out/readme.txt
keep cntkp cntex yr
rename cntkp cntkft
export excel using $out/`file'.xlsx, sheet(o`ogrp'.`o') firstrow(variables) sheetmodify keepcellfmt

* --------------------------------------------------
* ASSORTATIVE MATCHING
* --------------------------------------------------
local o = `o' + 1
shell echo "`ogrp'.`o' Assortative Matching (B5)">> $out/readme.txt

use `ft5yr', clear
keep if yr<=2010
collapse (mean) priorep20_ex, by(priorep20_kp)
drop if missing(priorep20_ex) | missing(priorep20_kp)
rename priorep20_ex prioreexavgbin
rename priorep20_kp priorekftbin
export excel using $out/`file'.xlsx, sheet(o`ogrp'.`o') firstrow(variables) sheetmodify keepcellfmt

* --------------------------------------------------
* FIRM AGE AND EXIT
* --------------------------------------------------
local o = `o' + 1
shell echo "`ogrp'.`o' FAge and Exit (B1)">> $out/readme.txt

use `firmpanel', clear
gen fdenom=(emp1f+emp2f)/2
gen fdeathemp = emp1f if exit==1
gen firms = 1
recodeFAge age fage
collapse (sum) fdeath = exit firms fdeathemp fdenom, by(yr fage)
drop if missing(fage)
preserve
gen fdr = 100*(fdeath /firms)
gen fdremp = 100*(fdeathemp /fdenom)
collapse (mean) fdr fdremp, by(fage)
export excel using $out/`file'.xlsx, sheet(o`ogrp'.`o') firstrow(variables) sheetmodify keepcellfmt

* --------------------------------------------------
* FIRM AGE AND GROWTH
* --------------------------------------------------
local o = `o' + 1
shell echo "`ogrp'.`o' FAge and Growth (B2, B3)">> $out/readme.txt

use  `firmpanel', clear
keep if exit==0
recodeFAge age fage
collapse (mean) mean_empgr=gr_emp (p48) median_empgr1=gr_emp (p52) median_empgr2=gr_emp (p88) pninty_empgr1=gr_emp (p92) pninty_empgr2=gr_emp (p8) pten_empgr1=gr_emp (p12) pten_empgr2=gr_emp  [aw=emp2f], by(fage)
drop if missing(fage)
egen median_empgr=rowmean(median_empgr1 median_empgr2)
egen pninty_empgr=rowmean(pninty_empgr1 pninty_empgr2)
egen pten_empgr=rowmean(pten_empgr1 pten_empgr2)
drop median_empgr1 median_empgr2 pninty_empgr1 pninty_empgr2 pten_empgr1 pten_empgr2
rename mean_empgr meanempgr 
rename median_empgr medianempgr 
rename pninty_empgr pnintyempgr
rename pten_empgr ptenempgr
export excel using $out/`file'.xlsx, sheet(o`ogrp'.`o') firstrow(variables) sheetmodify keepcellfmt

* --------------------------------------------------
* COX SURVIVAL
* --------------------------------------------------

use if gap>=-5 & gap<=5 & firmage_at_deathyr<=5 using `dthreg_fnl', clear
egen Inaics4yr = group(naics4 yr)
egen Inaics4 = group(naics4)
keep if gap>=1 & gap<=5
stset gap, failure(firmdeath) id(fid)

stcox treated lq1ppe_shockyr ln_emp_shockyr i.Ist i.Inaics4 i.yr i.firmage_shockyr, vce(cluster naics4) nohr basesurv(surv0)
local en = `e(N)'
local se = _se[treated]
local b = _b[treated] 

replace _t=0 if gap==0
bysort _t: keep if _n==1
gen double surv1=surv0^exp(`b')
replace surv0=1 if _t==0
replace surv1=1 if _t==0
keep _t surv0 surv1
rename _t yr_after_shock
drop if yr_after_shock==0
gen double stderr =  `se' if _n==1
gen double n =  `en' if _n==1
gen double b =  `b' if _n==1

local o = `o' + 1
shell echo "`ogrp'.`o' COX Survival (B7)">> $out/readme.txt
export excel using $out/`file'.xlsx, sheet(o`ogrp'.`o') firstrow(variables) sheetmodify keepcellfmt

* --------------------------------------------------
* PRIOR EARNINGS LOG OUTCOMES
* --------------------------------------------------
local o = `o' + 1
shell echo "`ogrp'.`o' Prior Earnings ln(emp) (TF1)">> $out/readme.txt
use `dthreg_fnl', clear
#delimit;
    reghdfe ln_emp
    i.post
    xdev_earn_post
    i.post#i.treated
    xdev_earn_treated_post
    if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
#delimit cr
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 1 `er2' `en' `file' `ogrp' `o'

local o = `o' + 1
shell echo "`ogrp'.`o' Prior Earnings ln(rev) (TF1)">> $out/readme.txt
use `dthreg_fnl', clear
#delimit;
    reghdfe ln_rev
    i.post
    xdev_earn_post
    i.post#i.treated
    xdev_earn_treated_post
    if firmage_at_deathyr<=5 & gap>=-5 & gap<=5, absorb(naics4#yr firmage fid) vce(cluster fid) keepsingletons version(3);
#delimit cr
local er2 = `e(r2)'
local en = `e(N)'
parmest, saving($out/hold.dta,replace)
saveRegs 1 `er2' `en' `file' `ogrp' `o'


di c(current_date) " " c(current_time)
log close
